<?php

namespace app\hongyanche\model;

// use think\Db;
// Db::listen(function($sql, $time, $explain){
//     // 记录SQL
//     echo $sql. ' ['.$time.'s]';
//     // 查看性能分析结果
//     dump($explain);
// });
// use think\Model;
class Ad
{
    private $user;
    private $user_id;
    private $isMarketingAdmin;

    function __construct()
    {
        $user = $this->user = model('user');
        $user_id = $this->user_id = $user->getUserId();
        $admin = model('admin')->is('marketing');
        $this->isMarketingAdmin = isset($admin['state']) ? $admin['state'] : 0;
    }

    //左侧导航
    public function ad_add($imgurl, $place, $home_url)
    {
        if (!$this->user_id) return array('state' => 0, 'errormsg' => '请登录');
        if (!$this->isMarketingAdmin) return ['state' => 0, 'errormsg' => 'authority incorrect'];
        if (!$imgurl) return ['state' => 0, 'errormsg' => '上传图片'];
        if (!$place) return ['state' => 0, 'errormsg' => '请选择一个位置'];
        //判断是否存在
        $res = db('marketing_ad')->where("place = $place and state != 0")->find();
        if ($home_url) {
            $data['home_url'] = $home_url;
        }
        if ($res) {
            $data['imgurl'] = $imgurl;
            $data['place'] = $place;
            $ress = db('marketing_ad')->where("ad_id = $res[ad_id]")->update($data);
            if (!$ress) return ['state' => 0, 'errormsg' => '修改失败'];
            return ['state' => 1, 'errormsg' => '成功'];
        }
        $data['create_time'] = time();
        $data['state'] = 1;
        $data['user_id'] = $this->user_id;
        $data['place'] = $place;
        $data['imgurl'] = $imgurl;
        $result = db('marketing_ad')->insertGetId($data);
        if (!$result) {
            return array('state' => 0, 'errormsg' => 'insert fail');
        }
//        加入action操作
        model('action')->add('ad_create', 'ad_id', $result);
        return array('state' => 1, 'errormsg' => 'ok', 'errorcode' => '',);
    }

    //开放关闭导航栏
    public function ad_release($ad_id, $release)
    {
        if (!$ad_id > 0) {
            return array('state' => 0, 'errormsg' => 'brand_id incorrect', 'errorcode' => '');
        }
        if ($release == 1) {
            $data['state'] = 1;
        } else {
            $data['state'] = 2;
        }
        $where = "ad_id in($ad_id)";
        $typeconfigstate = db('marketing_ad')->where($where)->update($data);
        if ($typeconfigstate) {
            return array('state' => 1, 'errormsg' => 'ok', 'errorcode' => '');
        }
        model('action')->add('ad_release', 'state', $release);
        return array('state' => 0, 'errormsg' => 'save error', 'errorcode' => '');
    }

    //导航删除
    public function ad_remove($ad_id)
    {
        if (!$this->user_id) return array('state' => 0, 'errormsg' => '请登录');
        if (!$ad_id) return array('state' => 0, 'errormsg' => '请选择一个导航栏');
        $res = db('marketing_ad')->where("ad_id in($ad_id)")->update(['state' => 0]);
        // 记录action表动作行为
        if ($res) {
            model('action')->add('ad_remove', 'ad_id', $ad_id);
            $ret = array('state' => 1, 'errormsg' => 'ok');
        } else {
            $ret = array('state' => 0, 'errormsg' => '删除失败');
        }
        return $ret;
    }

    //导航栏列表
    function ad_list()
    {
        $where = ['state' => 1];
        $res = db('marketing_ad')
            ->where($where)
            ->select();
        return ['state' => 1, 'data' => ['list' => $res]];
    }

}